<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AbstractController;
use App\Service\AmountService;
use App\Utils\ApiUtils;
use App\Utils\UploadUtil;
use App\Utils\StringUtil;
use App\Validate\AmountValidation;
use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
class AmountController extends AbstractController
{

    /**
     * @Inject
     * @var AmountService
     */
    protected $service;

    /**
     * 系统设置-充值配置-列表信息
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function typeList()
    {
        $data = $this->service->typeList();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 系统设置-充值配置-新增
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function typeAdd()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $name = $this->request->input('name', "");
        $status = (int) $this->request->input('status', 1);
        $link_type = (int) $this->request->input('link_type', 3);
        $explain = $this->request->input('explain', "");
        $explain_detail = $this->request->input('explain_detail', "");
        //处理富文本内容
        if ($link_type == 2 && !empty($explain_detail)) {
            $explain_detail = StringUtil::handleDescriptionHtml($explain_detail);
        }
        $this->service->typeAdd(compact('name', 'status', 'link_type', 'explain_detail', 'explain'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-充值配置-编辑
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function typeEdit()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $id = (int) $this->request->input('id', 0);
        $name = $this->request->input('name', "");
        $status = (int) $this->request->input('status', 1);
        $link_type = (int) $this->request->input('link_type', 3);
        $explain = $this->request->input('explain', "");
        $explain_detail = $this->request->input('explain_detail', "");
        //处理富文本内容
        if ($link_type == 2 && !empty($explain_detail)) {
            $explain_detail = StringUtil::handleDescriptionHtml($explain_detail);
        }
        if ($id == 1 && strpos($name, '快充') === false) {
            return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '操作失败,基础数据限制id：1昵称必须包含快充'));
        }
        if ($id == 2 && strpos($name, '慢充') === false) {
            return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '操作失败,基础数据限制id：2昵称必须包含慢充'));
        }
        $this->service->typeEdit(compact('id', 'name', 'status', 'link_type', 'explain_detail', 'explain'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-充值配置-删除
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function typeDel()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->typeDel($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-充值金额-列表信息
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    function list()
    {

        $data = $this->service->amountList();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 系统设置-充值金额-新增-配置信息
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function amountAddInfo()
    {
        $data = $this->service->typeList();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $data));
    }

    /**
     * 系统设置-充值金额-新增
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function amountAdd()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $amount = (int) $this->request->input('amount', 0);
        $ori_amount = (int) $this->request->input('ori_amount', 0);
        $type_id = (int) $this->request->input('type_id', 1);
        $status = (int) $this->request->input('status', 1);
        $this->service->amountAdd(compact('amount', 'ori_amount', 'type_id', 'status'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-充值金额-编辑
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function amountEdit()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $id = (int) $this->request->input('id', 0);
        $amount = (int) $this->request->input('amount', 0);
        $ori_amount = (int) $this->request->input('ori_amount', 0);
        // $sku=$this->request->input('sku',0);
        $type_id = (int) $this->request->input('type_id', 1);
        $status = (int) $this->request->input('status', 1);
        $this->service->amountEdit(compact('id','amount', 'ori_amount', 'type_id', 'status'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '编辑成功'));
    }

    // /**
    //  * 系统设置-充值金额-禁用
    //  * @Author czm
    //  * @DateTime 2020-04-18
    //  * @return void
    //  */
    // public function amountForbid()
    // {
    //     $id = (int) $this->request->input('id', 0);
    //     if (empty($id)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '参数为空'));
    //     }
    //     $info = RechargeAmount::find($id);
    //     if (empty($info)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '充值金额信息为空'));
    //     }
    //     $logInfo = $info->toJson(JSON_UNESCAPED_UNICODE);
    //     $info->status = 2;
    //     $info->save();
    //     //添加管理员操作日记
    //     $this->adminLogLogic->addLog('禁用了充值面额' . $logInfo);
    //     return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '禁用成功'));
    // }

    /**
     * 系统设置-充值金额-删除
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function amountDel()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->amountDel($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    // /**
    //  * 系统设置-充值金额-启用
    //  * @Author czm
    //  * @DateTime 2020-04-18
    //  * @return void
    //  */
    // public function amountOpen()
    // {
    //     $id = (int) $this->request->input('id', 0);
    //     if (empty($id)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '参数为空'));
    //     }
    //     $info = RechargeAmount::withoutGlobalScope('is_del')->find($id);
    //     if (empty($info)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '充值金额信息为空'));
    //     }
    //     $logInfo = $info->toJson(JSON_UNESCAPED_UNICODE);
    //     $info->status = 1;
    //     $info->is_del = 1;
    //     $info->save();
    //     //添加管理员操作日记
    //     $this->adminLogLogic->addLog('启用了充值面额' . $logInfo);
    //     //修正金额状态
    //     return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '删除成功'));
    // }

    /**
     * 系统设置-充值金额-编辑金额渠道
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function channelEdit()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->channelEdit($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 油卡配置-充值金额列表信息
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function oilList()
    {
        $pageSize = $this->request->input('pageSize', 15);
        $page = $this->request->input('page', 1);
        $params = array_filter(compact('pageSize', 'page'));
        $result = $this->service->oilList($params);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));
    }

    /**
     * 油卡配置-充值金额新增
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function oilAdd()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->oilAdd($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }
    /**
     * 油卡配置-充值金额编辑
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function oilEdit()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->oilEdit($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 油卡配置-充值金额删除
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function oilDel()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->oilDel($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 油卡配置-编辑金额渠道
     * @Author czm
     * @DateTime 2020-04-18
     * @return void
     */
    public function oilChannel()
    {
        make(AmountValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->oilChannel($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }


    //rpc的单元测试
    public function rpcTest(){
        $a=make(\App\JsonRpc\ServiceProvider\AmountService::class)->consume(json_encode(['ori_amount'=>9.9,'amount'=>10,'type_id'=>1,'type'=>1]));
        $b=make(\App\JsonRpc\ServiceProvider\CouponService::class)->consume(json_encode(['openid'=>'ou8usjumSTYkVDMYTI_hbG1SInfY','coupon_id'=>1,'type_id'=>1,'type'=>1]));
        $c=make(\App\JsonRpc\ServiceProvider\CouponService::class)->consume(json_encode(['openid'=>'ou8usjumSTYkVDMYTI_hbG1SInfY','coupon_id'=>1,'type_id'=>1,'type'=>2]));
        $d=make(\App\JsonRpc\ServiceProvider\AmountService::class)->consume(json_encode(['ori_amount'=>9.9,'amount'=>10,'type_id'=>1,'type'=>2]));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, 'ces',[$a,$b,$c,$d]));
    }
}
